package com.ourteam.product.model.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.GridBean;
import com.ourteam.product.model.dao.ProductModelObjectDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelObjectBean;
import com.ourteam.product.model.service.IProductModelService;
import com.ourteam.product.model.service.ProductModelServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/MODEL/productModelDataType")
public class ProductModelDataTypeAPI extends AbstractServiceController {

	private static final IProductModelService PRODUCT_MODEL_SERVICE = ProductModelServiceFactory
			.getProductModelService();

	@RequestMapping("/saveDataType")
	@ResponseBody
	public ProductModelObjectBean saveDataType(@ModelAttribute ProductModelObjectBean modelObjectBean,
			HttpServletRequest request, Model model) throws Exception {

		modelObjectBean.setObjectType("DATA_TYPE");

		if (modelObjectBean.getId() != 0) {
			PRODUCT_MODEL_SERVICE.modifyProductModelObject(modelObjectBean);
		} else {
			PRODUCT_MODEL_SERVICE.addNewProductModelObject(modelObjectBean);
		}

		return modelObjectBean;
	}
	
	@RequestMapping("/deleteDataType")
	@ResponseBody
	public boolean deleteDataType(@RequestParam("ids") long[] ids) throws Exception {

		PRODUCT_MODEL_SERVICE.deleteProductModelObjects(ids);

		return true;
	}
	
	@RequestMapping("/queryDataTypeList")
	@ResponseBody
	public GridBean queryDataType(final @ModelAttribute ProductModelObjectSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		searchModel.setObjectType("DATA_TYPE");

		AbstractSearchHelper<ProductModelObjectDAOQueryBean, ProductModelObjectSearchModel> searchHelper = new AbstractSearchHelper<ProductModelObjectDAOQueryBean, ProductModelObjectSearchModel>(
				searchModel) {

			@Override
			protected ProductModelObjectDAOQueryBean createQueryBean() {
				return new ProductModelObjectDAOQueryBean();
			}

			@Override
			protected int queryCount(ProductModelObjectDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductModelObjectCount(queryBean);
			}

			@Override
			protected Object[] queryData(ProductModelObjectDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductModelObjects(queryBean);
			}

		};

		return searchHelper.doSearch(request);

	}
}
